package com.baosight.sczx.zm.ck.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.cg.domain.ZMCGContactList;
import com.baosight.sczx.zm.cg.domain.ZMCGContactListFeedBack;
import com.baosight.sczx.zm.cg.domain.ZMCGContactReceive;
import com.baosight.sczx.zm.ck.domain.ZMCKBL;
import com.baosight.sczx.zm.ck.domain.ZMCKFH;
import com.baosight.sczx.zm.ck.domain.ZMCKKCSB;
import com.baosight.sczx.zm.ck.domain.ZMCKPSSB;
import com.baosight.sczx.zm.ck.domain.ZMCKRJGGL;
import com.baosight.sczx.zm.ck.domain.ZMCKYJSF;
import com.baosight.sczx.zm.cp.domain.ZMCPProduceProblem;

/**
 * 业务联系单
 * <p>Title: ServiceZMCF25.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-7-6 上午07:44:02  严洪浩  Create
 *</pre>
 */
public class ServiceZMCK30 extends ServiceEPBase {

	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF25</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午01:09:11 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.addBlock(getMines());
		return outInfo;
	}

	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF25</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午01:09:01 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		
		if(null != inInfo.getBlock("inqu_status")){
			String fdYjsfName = inInfo.getBlock("inqu_status").getCellStr(0, "fdYjsfName");
			String fdMiningId = inInfo.getBlock("inqu_status").getCellStr(0, "fdMiningId");
			if(null != fdYjsfName && !("").equals(fdYjsfName)){
				StaticTools.setQueryParameters(inInfo, "fdYjsfName", fdYjsfName, true);//添加查询条件 
			}
			if(null != fdMiningId && !("").equals(fdMiningId)){
				StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);//添加查询条件 
			}
			}		
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCKYJSF.query", new ZMCKYJSF());
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(getMines());
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		return outInfo;
	}
	
	
	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCGTsPg.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}

	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:07:57 张坤
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 		
		String date = StaticTools.getDate();
		String fdStatus = "草拟";
		String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
		ZMCKYJSF _dom = new ZMCKYJSF();
//		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
//		String fdMiningName = (String) UserSession.getInSessionProperty("miningName");//矿名称的默认值
		String fdReporterId = (String) UserSession.getUserId();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdCurrentDate(date);
			_dom.setFdReportPerson(fdReportPerson);
			_dom.setFdReporterId(fdReporterId);
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
			_dom.setFdStatus(fdStatus);
			_dom.setFdReportDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CK_YJSF"));
			this.dao.insert("ZMCKYJSF.insert",_dom); 
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:20:50 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验是否重复
		EiInfo out=new EiInfo();
		String date = StaticTools.getDate();
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		String fdMiningName = (String) UserSession.getInSessionProperty("miningName");//矿名称的默认值
		String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
		String fdReporterId = (String) UserSession.getUserId();
		ZMCKYJSF _dom = new ZMCKYJSF();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			_dom.setFdCurrentDate(date);
			_dom.setFdReportPerson(fdReportPerson);
			_dom.setFdMiningId(fdMiningId);
			_dom.setFdMiningName(fdMiningName);
            _dom.setFdReporterId(fdReporterId);
            _dom.setFdReportDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
            _dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());
			this.dao.update("ZMCKYJSF.update",_dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能修改草拟状态的记录!"); 
				return out;
			}
			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
		
	}	

	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:30:17 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) { 
		EiInfo out=new EiInfo(); 
		ZMCKYJSF _dom = new ZMCKYJSF();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			this.dao.delete("ZMCKYJSF.delete", _dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能删除草拟状态的记录!"); 
				return out;
			}
		}
		EiInfo outInfo = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return outInfo;

	}
	
	

	/**
	 * <p>Function: execute</p>
	 * <p>Description: 执行</p>
	 *
	 */
	
	public EiInfo execute(EiInfo inInfo) { 
		EiInfo out=new EiInfo(); 
		ZMCKYJSF _dom = new ZMCKYJSF();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		String date;
		String nf;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
				date = _dom.getFdCurrentDate();
				nf = date.substring(0,date.indexOf("-"));
				get1table(nf);
				get2table(nf);
				get3table(nf);
				get4table(nf);
				get5table(nf);	
				}else{
					out = this.query(inInfo);
					out.setStatus(-1);
					out.setMsg("只能执行草拟状态的记录!"); 
					return out;
				}
		}
		EiInfo outInfo = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
		
	}
	
	/**
	 * 将符合条件的同年份的防洪设施检查表中的记录插入到生产问题表中
	 */
	
	private void get1table(String nf)
	{		
		EiInfo inInfo=new EiInfo();
		 if(null != nf && !("").equals(nf)){
		    	String fdBeginDate = nf+"-01-01";
		    	String fdEndDate = nf+"-12-31";
		    	StaticTools.setQueryParameters(inInfo, "fdBeginDate", fdBeginDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdEndDate", fdEndDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdStatus", "上报", true);//添加查询条件 
		    	
		    }
		EiInfo outInfo = super.query(inInfo, "ZMCKFH.query", new ZMCKFH());
		ZMCKFH _dom = new ZMCKFH();
		ZMCPProduceProblem _dom1 = new ZMCPProduceProblem();
		EiBlock eiBlock = outInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		long fdStatus = 1;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom1.setFdProblemDescription(_dom.getFdFhssProblem());
			_dom1.setFdProblemDate(_dom.getFdCurrentDate());
			_dom1.setFdBusinessMan(_dom.getFdPeopleName());
			_dom1.setFdBusinessDepartmentId(_dom.getFdDepartmentId());	
//			_dom1.setFdProblemOrigin("雨季三防防洪设备检查");
			_dom1.setFdProblemStatus(fdStatus);
			_dom1.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPROBLEM"));
						
			this.dao.insert("ZMCPProduceProblem.insert",_dom1); 
		}
	}
	
	

	/**
	 * 将符合条件的同年份的避雷装置检查表中的记录插入到生产问题表中
	 */
	
	private void get2table(String nf)
	{		
		EiInfo inInfo=new EiInfo();
		 if(null != nf && !("").equals(nf)){
		    	String fdBeginDate = nf+"-01-01";
		    	String fdEndDate = nf+"-12-31";
		    	StaticTools.setQueryParameters(inInfo, "fdBeginDate", fdBeginDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdEndDate", fdEndDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdStatus", "上报", true);//添加查询条件 
		    	
		    }
		EiInfo outInfo = super.query(inInfo, "ZMCKBL.query", new ZMCKBL());
		ZMCKBL _dom = new ZMCKBL();
		ZMCPProduceProblem _dom1 = new ZMCPProduceProblem();
		EiBlock eiBlock = outInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		long fdStatus = 1;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom1.setFdProblemDescription(_dom.getFdFhssProblem());
			_dom1.setFdProblemDate(_dom.getFdCurrentDate());
			_dom1.setFdBusinessMan(_dom.getFdPeopleName());
			_dom1.setFdBusinessDepartmentId(_dom.getFdDepartmentId());			
			_dom1.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPROBLEM"));
			_dom1.setFdProblemStatus(fdStatus);		
			this.dao.insert("ZMCPProduceProblem.insert",_dom1); 
		}
	}
	

	/**
	 * 将符合条件的同年份的库存水泵检查表中的记录插入到生产问题表中
	 */
	
	
	private void get3table(String nf)
	{		
		EiInfo inInfo=new EiInfo();
		 if(null != nf && !("").equals(nf)){
		    	String fdBeginDate = nf+"-01-01";
		    	String fdEndDate = nf+"-12-31";
		    	StaticTools.setQueryParameters(inInfo, "fdBeginDate", fdBeginDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdEndDate", fdEndDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdStatus", "上报", true);//添加查询条件 
		    	
		    }
		EiInfo outInfo = super.query(inInfo, "ZMCKKCSB.query", new ZMCKKCSB());
		ZMCKKCSB _dom = new ZMCKKCSB();
		ZMCPProduceProblem _dom1 = new ZMCPProduceProblem();
		EiBlock eiBlock = outInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		long fdStatus = 1;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom1.setFdProblemDate(_dom.getFdCurrentDate());
			_dom1.setFdBusinessMan(_dom.getFdPeopleName());
			_dom1.setFdBusinessDepartmentId(_dom.getFdDepartmentId());			
			_dom1.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPROBLEM"));
			_dom1.setFdProblemStatus(fdStatus);			
			this.dao.insert("ZMCPProduceProblem.insert",_dom1); 
		}
	}
	

	/**
	 * 将符合条件的同年份的排水设备检查表中的记录插入到生产问题表中
	 */
	
	
	private void get4table(String nf)
	{		
		EiInfo inInfo=new EiInfo();
		 if(null != nf && !("").equals(nf)){
		    	String fdBeginDate = nf+"-01-01";
		    	String fdEndDate = nf+"-12-31";
		    	StaticTools.setQueryParameters(inInfo, "fdBeginDate", fdBeginDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdEndDate", fdEndDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdStatus", "上报", true);//添加查询条件 
		    	
		    }
		EiInfo outInfo = super.query(inInfo, "ZMCKPSSB.query", new ZMCKPSSB());
		ZMCKPSSB _dom = new ZMCKPSSB();
		ZMCPProduceProblem _dom1 = new ZMCPProduceProblem();
		EiBlock eiBlock = outInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		long fdStatus = 1;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom1.setFdProblemDate(_dom.getFdCurrentDate());
			_dom1.setFdBusinessMan(_dom.getFdPeopleName());
			_dom1.setFdBusinessDepartmentId(_dom.getFdDepartmentId());			
			_dom1.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPROBLEM"));
			_dom1.setFdProblemStatus(fdStatus);			
			this.dao.insert("ZMCPProduceProblem.insert",_dom1); 
		}
	}
	

	/**
	 * 将符合条件的同年份的入井管轨缆接地装置检查表中的记录插入到生产问题表中
	 */
	
	private void get5table(String nf)
	{		
		EiInfo inInfo=new EiInfo();
		 if(null != nf && !("").equals(nf)){
		    	String fdBeginDate = nf+"-01-01";
		    	String fdEndDate = nf+"-12-31";
		    	StaticTools.setQueryParameters(inInfo, "fdBeginDate", fdBeginDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdEndDate", fdEndDate, true);//添加查询条件 
		    	StaticTools.setQueryParameters(inInfo, "fdStatus", "上报", true);//添加查询条件 
		    	
		    }
		EiInfo outInfo = super.query(inInfo, "ZMCKRJGGL.query", new ZMCKRJGGL());
		ZMCKRJGGL _dom = new ZMCKRJGGL();
		ZMCPProduceProblem _dom1 = new ZMCPProduceProblem();
		EiBlock eiBlock = outInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		long fdStatus = 1;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom1.setFdProblemDescription(_dom.getFdFhssProblem());
			_dom1.setFdProblemDate(_dom.getFdCurrentDate());
			_dom1.setFdBusinessMan(_dom.getFdPeopleName());
			_dom1.setFdBusinessDepartmentId(_dom.getFdDepartmentId());			
			_dom1.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPROBLEM"));
			_dom1.setFdProblemStatus(fdStatus);			
			this.dao.insert("ZMCPProduceProblem.insert",_dom1); 
		}
	}
	/**
	 * <p>Function: validate</p>
	 * <p>Description: 对主表记录新增，删除，上报前的后台校验</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:40:23张坤
	 * </p>
	 */
	private boolean validate(ZMCKYJSF _dom){
		  String fdStatus = _dom.getFdStatus();
		  if("草拟".equals(fdStatus)){
			 return true; 
		  }else
			  return false;
		  
	}

}